import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { SFSchema, SFComponent } from '@delon/form';
import { NzMessageService } from 'ng-zorro-antd';
import {HttpService, GridComponent} from 'lbf';
import { STColumn } from '@delon/abc';
import {ReuseTabService} from '@delon/abc/reuse-tab';

@Component({
  selector: 'app-xtgl-xxfs',
  templateUrl: './xxfs.component.html',
  styleUrls: ['./xxfs.less']
})
export class XtglXxfsComponent implements OnInit {

  value=''
  condition= null

  searchValue = ''
  nodes = []
  params = {sname: 'xxgl_yhcx',queryparas: {}}
  treeData:any;
  yhList = []
  flag= false;

  isVisible = false;
  isTsVisible = false;
  editSchema: SFSchema = {
    properties: {
      custom: {
        type: 'string',
        title: '收件人',
        ui: {
          widget: 'custom'
        }
      },
      XTITLE: {
        type: 'string',
        title: '标题',

      },
      XCONTENT: {
        type: 'string',
        title: '正文',
        ui: {
          widget: 'textarea',
          autosize: { minRows: 21}
        }
      }
    },
    ui: {
      spanLabelFixed:50,
      grid: {
        span: 24
      }
    }
  }

  yhColumns: STColumn[] = [
    
    {title: '登录名',index: 'UNAME'},
    {title: '用户名',index: 'NAME'},
    {title: '手机号码',index: 'PHONE'},
    {
      title: '操作区',
      buttons: [
        {
          text: '添加',
          click: (record) => this.yhAdd(record)
        }
      ]
    }
  ]
  
  @ViewChild('st',{static:false}) st: GridComponent;
  @ViewChild('sf',{static:false}) sf: SFComponent;
  constructor(private http: _HttpClient,private msgSrv: NzMessageService,private httpService: HttpService,
    private reuseService: ReuseTabService) { }

  ngOnInit() { 
    this.initGwTree();
  }

  // 初始化岗位树
  initGwTree(){
    this.httpService.lbservice('xxgl_gwlist',{}).then(res=>{
      console.log(res);
      this.nodes = res.message.list;
    });
 }

  nzEvent(event){
    this.treeData = event.node.origin;
    console.log(this.treeData)
    if(this.treeData.hasOwnProperty('dgb010')){
      this.st.reload({DGB010: this.treeData.dgb010})
    }
    if(this.treeData.hasOwnProperty('dgb020')){
      this.st.reload({DGB020: this.treeData.dgb020})
    }
    if(this.treeData.hasOwnProperty('dgb040')){
      this.st.reload({DGB040: this.treeData.dgb040})
    }
    if(this.treeData.hasOwnProperty('roleid')){
      this.st.reload({ROLEID: this.treeData.roleid})
    }
  }
 
  add(){
    this.isVisible = true;
    
  }

  yhAdd(record){
    this.flag = false;
    console.log(record)
    const yh = {label: record.NAME,value: record.USERID};
    
    if(this.yhList.length === 0 ) {
      this.yhList.push(yh);
    }else{
      
      this.yhList.forEach(element => {
        if(element.value === yh.value){
          this.flag = true;
        }
      });
      if(this.flag === false){
        this.yhList.push(yh);
      }else{
        this.msgSrv.info(yh.label+'已被添加')
      }
    }   
    console.log(this.yhList)
  }

  saveAdd(){
    let yh = ''
    this.yhList.forEach(ele => {
      yh = yh + ele.label+';  '
    })
    this.value = yh;
    this.isVisible = false;
  }

  handleCancel(){
    this.isVisible = false;
  }

  send(){
    if(this.yhList.length === 0 ){
      this.msgSrv.error('请填写收件人之后再发送');
      return;
    }
    if(this.sf.value.XTITLE === undefined || this.sf.value.XTITLE ===''){
     this.isTsVisible = true
    }else{
      this.confirm();
    }
  }

  handleClose(item){
    console.log(item)
    this.yhList = this.yhList.filter(data => data.value!==item.value)
    console.log(this.yhList)
  }

  formChange(event){
    if(event.hasOwnProperty('XTITLE')){
      this.condition = event.XTITLE;
      if(this.condition === ''){
        this.condition = null;
      }
    }
  }

  confirm(){
    this.isTsVisible = false;
    const uids = []
    this.yhList.forEach(ele => {
      uids.push(ele.value)
    })
    if(this.sf.value.XTITLE === undefined || this.sf.value.XTITLE ==='') this.sf.value.XTITLE= ' ';

    if(this.sf.value.XCONTENT === undefined || this.sf.value.XCONTENT ==='') this.sf.value.XCONTENT = ' ';

    this.httpService.lbservice('XXGL_XXFS',{YHLIST: uids,MESSAGE: this.sf.value}).then(res => {
      if(res.code > 0 ) {
        this.msgSrv.success('发送成功');
        
        setTimeout(() => {
          this.reuseService.replace('/xxgl/xxfswh');
        }, 1000);
      };
    })
  }

  cancel(){
    this.isTsVisible = false;
  }

  close(){
    this.reuseService.replace('/xxgl/xxfswh');
  }
}
